<template>
	<view class="content">
		<image :src="$store.state.common.systemInfo.fileurl + 'avatar/' + item.fid + '.jpg'" style="height: 100vh;"
			mode="heightFix"></image>
		<view class="ftbg">
			<view style="margin-top: 280rpx;">
				<u-avatar :src="$store.state.common.systemInfo.fileurl + 'avatar/' + item.fid + '.jpg'" size="100"
					style="margin: 10rpx;" mode="square"></u-avatar>
			</view>
			<view style="margin-top: 10rpx;color: white;font-size: 12rpx;">
				{{item.name}}
			</view>
			<view style="margin-top: 130rpx;color: white;">
				正在等待对方回应...
			</view>
		</view>

		<view
			style="position: fixed;bottom: 100rpx;left: 0rpx;width: 100%;display: flex;justify-content: center;align-items: center;">
			<image @click="cancel" src="/static/images/icon_guad.png"
				style="width: 120rpx;height: 120rpx;border-radius: 60rpx;" mode=""></image>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import {
		pub,
		sub,
		unsub
	} from '@/common/event.js'
	import utils from '../../utils/index';
	export default {
		data() {
			return {
				id: '',
				mid: ''
			}
		},
		computed: {
			...mapState({
				item: function(state) {
					return state.friend.list.find(e => e.fid == this.id)
				}
			})
		},
		onBackPress(options) {
			console.log(147, options)
			if (options.from && options.from == 'backbutton') {
				return true
			}
			return false
		},
		onUnload() {
			this.$store.commit(`common/setStatus`, "normal")
			unsub("socket_touser_audiocallrefuse_" + this.mid, this.refusesub)
			unsub("socket_touser_audiocallbusy_" + this.mid, this.refusebusy)
			unsub("socket_touser_audiocallagree_" + this.mid, this.agreeb)

		},
		onLoad({
			id,
			mid
		}) {
			this.id = id
			this.mid = mid
			this.sendToUser()
			sub("socket_touser_audiocallrefuse_" + mid, this.refusesub)
			sub("socket_touser_audiocallbusy_" + mid, this.refusebusy)
			sub("socket_touser_audiocallagree_" + mid, this.agreeb)

		},
		methods: {
			sendToUser: async function() {
				this.$socket.send({
					type: "touser_audiocall",
					data: {
						to: {
							id: this.id
						},
						user: this.$store.state.user.user,
						mid: this.mid
					}
				})
			},
			refusebusy: function() {
				pub("stopmusic", {})
				uni.showModal({
					title: "连接取消",
					content: "对方正在通话中",
					showCancel: false,
					success() {
						uni.navigateBack()
					}
				})
			},
			refusesub: function() {
				pub("stopmusic", {})
				uni.showModal({
					title: "挂断",
					content: "对方挂断了语音通话",
					showCancel: false,
					success() {
						uni.navigateBack()
					}
				})
			},
			cancel: async function() {
				const ret = await this.$u.put(`chat/chat/changemessageinfo/${this.mid}`, {
					status: '已取消'
				})
				this.$store.commit(`common/setStatus`, "normal")
				this.$socket.send({
					type: "touser_audiocallcancel",
					data: {
						to: {
							id: this.id
						},
						user: this.$store.state.user.user,
						mid: this.mid
					}
				})
				uni.navigateBack()
			},
			agreeb: async function() {
				this.$store.commit(`common/setStatus`, "videocall")
				// #ifdef APP
				uni.redirectTo({
					url:"/pages/friend/audiocall?id="+this.id
				})
				// #endif
				// #ifdef H5
				uni.redirectTo({
					url:"/pages/friend/audiocallh5?id="+this.id
				})
				// #endif
			},
		}
	}
</script>

<style scoped>
	.content {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background: linear-gradient(to bottom, #666, #222);
	}

	.ftbg {
		width: 100%;
		height: 100vh;
		top: 0rpx;
		left: 0rpx;
		background: #00000099;
		backdrop-filter: blur(20px);
		position: fixed;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
</style>